package com.gupt.reggie.controller;/*
@author name
@data 2023/4/20-17:52
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gupt.reggie.common.R;
import com.gupt.reggie.dto.DishDto;
import com.gupt.reggie.entity.Dish;
import com.gupt.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("新增菜品");

        dishService.saveWithFlavor(dishDto);

        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<DishDto> dishDtoPage = dishService.page(page,pageSize,name);
        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    @CacheEvict(value = "DishCache",allEntries = true)  //菜品修改，删除所有缓存数据
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("修改菜品：{}",dishDto.toString());

        dishService.updateWithFlavor(dishDto);

//        //清理所有菜品的缓存数据
//        Set keys = redisTemplate.keys("dish_*");
//        Long delete = redisTemplate.delete(keys);
//        log.info("清理缓存数据返回值:{}",delete);
//
//        //精确清理某一个类型的缓存数据
//       String key = "dish_" + dishDto.getCategoryId() + "_1";
//       redisTemplate.delete(key);
        return R.success("修改菜品成功");
    }



    /**
     * 根据菜品类型查看具体菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){ //相比Long categoryId 扩展性更高一点
        List<DishDto> dishDtoList = dishService.list(dish);
        return R.success(dishDtoList);
    }

    /**
     * 修改菜品状态
     * @param id
     * @param ids
     * @return
     */
    @PostMapping("/status/{id}")
    @CacheEvict(value = "DishCache",allEntries = true)  //菜品状态修改，删除所有缓存数据
    public R<String> save(@PathVariable Integer id,@RequestParam List<Long> ids){ //ids 菜品id
        Dish dish = new Dish();
        dish.setStatus(id);
        // UPDATE dish  SET status = 'id' WHERE id = 'ids';
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        dishService.update(dish,queryWrapper);

//        //查询当前哪个分类id,修改状态后更新分类缓存
//        List<Dish> dishes = dishService.list(queryWrapper);
//        for(Dish item: dishes){
//            Long categoryId = item.getCategoryId();
//            //精确清理某一个菜品的缓存数据
//            String key = "dish_" + categoryId + "_1";
//            redisTemplate.delete(key);
//        }
        return R.success("修改成功");
    }

    @DeleteMapping()
    @CacheEvict(value = "DishCache",allEntries = true)  //菜品删除，删除所有缓存数据
    public R<String> removeById(@RequestParam List<Long> ids){
        dishService.deleteDishById(ids);
        return R.success("删除成功");
    }
}
